package com.sb.configtion;

import com.sb.async.SpringAsyncConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * Created by wujunzhou on 2017/11/1.
 */
@Component
public class SpringUtil implements ApplicationContextAware{
    private  final Logger logger= LoggerFactory.getLogger(SpringUtil.class);

    private static ApplicationContext applicationContext = null;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if(SpringUtil.applicationContext == null){
            SpringUtil.applicationContext  = applicationContext;
        }
        logger.info("---------------com.sb.configtion.SpringUtil---------------");
    }
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }
    //通过name,以及Clazz返回指定的Bean
    public static <T> T getBean(String name,Class<T> clazz){
        return getApplicationContext().getBean(name, clazz);
    }
}
